<?php

/*
 * @version		$Id: Komento_plugin.php 2.3.0 2014-06-21 $
 * @package		Joomla
 * @copyright   Copyright (C) 2012-2014 MrVinoth
 * @license     GNU/GPL http://www.gnu.org/licenses/gpl-2.0.html
*/

// no direct access
defined('_JEXEC') or die('Restricted access');

require_once JPATH_ROOT . '/components/com_komento/komento_plugins/abstract.php';

class KomentoComAllvideoshare extends KomentoExtension	{

	public $_item;

    public $_map = array(
        'id'            => 'id',
        'title'         => 'title',
        'hits'          => 'views',
        'created_by'    => 'user',
        'catid'         => 'category',
        'permalink'    => 'permalink_field'
        );

    public function __construct( $component )    {
	        parent::__construct( $component );
    }
	
		
    public function load( $cid )    {
        static $instances = array();
		
       if(!isset($instances[$cid]))   {
	  $db = JFactory::getDbo();
      $query = $db->getQuery(true); 
	  $query = "SELECT * FROM #__allvideoshare_videos WHERE published=1 AND id=".(int)$cid; 
      $db->setQuery((string)$query);
      $this->_item = $db->loadObject();	 
	  
	  $qs = '';
	  $qs .= JRequest::getCmd('orderby') ? '&orderby=' . JRequest::getCmd('orderby') : '';
	  $qs .= JRequest::getInt('Itemid')  ? '&Itemid=' . JRequest::getInt('Itemid') : '';
	  
	  $this->_item->permalink_field = JRoute::_("index.php?option=com_allvideoshare&view=video&slg=".$this->_item->slug.$qs);	  
	   $instances[$cid] = $this->_item;	   
   	} 
      $this->_item = $instances[$cid];
      return $this;
        
    }
	
	public function getContentId()
	{
		return $this->_item->{$this->_map['id']};
	}

	
	public function getContentHits()
	{
		return $this->_item->{$this->_map['hits']};
	}

	
	public function getAuthorId()
	{
		return $this->_item->{$this->_map['created_by']};
	}
	
    public function getContentIds( $categories = '' )
    {
     /* $db = JFactory::getDbo();
      $query = $db->getQuery(true);
      $query->select('a.id, a.title');
      $query->from($db->quoteName('#__allvideoshare_videos').' AS a');
      $query->join('LEFT', '#__allvideoshare_categories AS c ON c.name = a.category');
      //$query->order('a.id');
 
      if(!empty($categories))
      {
         if(is_array($categories))
         {
            $categories = implode(',', $categories);
         }
         // with category filters
         $query->where('a.category IN '.$db->quote($categories));
       }
 
      $db->setQuery((string)$query);	
	
       return $db->loadResultArray();*/
	   return;
		
   }

    public function getCategories(){	
      			
        return;
    }

    // to determine if is listing view
    public function isListingView()
    {
		$views = array('videos', 'categories' );		
		return in_array(JRequest::getCmd('view'), $views);
        
    }

    // to determine if is entry view
    public function isEntryView()
    {
		 return JRequest::getCmd('view') == 'video';
    }

    public function onExecute( &$article, $html, $view, $options = array() )
    {
        // $html is the html content generated by Komento
		$config = Komento::getConfig( 'com_allvideoshare' );
		$model = Komento::getModel('comments');
     	$count = $model->getCount($this->component, $this->getContentId());
      	$article->numOfComments = $count;
		
			
        return $html;
    }
	
	//Load backend Details
	
	public function getComponentIcon()
	{
  		 return './components/com_allvideoshare/assets/logo.png';
	}
 
	public function getComponentName()
	{
  		 return 'Allvideoshare';
	}
	
			
}